package com.dayu.gulimall.cart.controller;

import com.dayu.gulimall.cart.intercepter.CartIntercepter;
import com.dayu.gulimall.cart.service.CartService;
import com.dayu.gulimall.cart.vo.Cart;
import com.dayu.gulimall.cart.vo.UserInfoTo;
import com.dayu.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author dayu
 * @version 1.0
 * @email dayu007@foxmail.com
 * @date 2021/3/27 12:35
 * @description 浏览器 有个cookie ：user-key，标识用户身份，一个月后过期。
 * 如果第一次使用jd的购物车功能，都会给一个临时用户身份，浏览器以后保存，每次访问都会带上这个用户身份。
 * 登录：session有
 * 没登录：按照cookie里面带来user-key来做。
 * 第一次：没有临时用户，帮忙创建一个临时用户身份。
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems() throws ExecutionException, InterruptedException {
        return cartService.getCurrentUserItems();
    }



    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check) {
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num) {
        cartService.countItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        UserInfoTo cartInfoTo = CartIntercepter.threadLocal.get();
        Cart cart=cartService.getCart();
        model.addAttribute("cart",cart);
        System.out.println(cartInfoTo);
        return "cartList";
    }

    /**
     * 添加购物车
     *
     * @param skuId
     * @param num
     * @param redirectAttributes 重定向跳转
     *                           redirectAttributes.addFlashAttribute("skuId",skuId);将数据放在session里面，但是只能取一次
     *                           redirectAttributes.addAttribute();将数据放在url后面，使用重定向，自动在url后面封装拼接参数
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {
        System.out.println(skuId);
        CartItem cartItem = cartService.getCartItemInfo(skuId);
        model.addAttribute("item", cartItem);
        return "success";
    }

    @GetMapping("/getCart.html")
    public Cart getCart(Model model) {
        return null;
    }

}
